﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Gamekit2D;
using FairyGUI;
using System;

public class GetInput : MonoBehaviour
{
    // Start is called before the first frame update
    private string sceneName = "";
    private AudioSource audiosource; 
    void Awake()
    {
        //清空之前ui
        if (LoadingPanel._sceneToLoad != "")
        {
            sceneName = LoadingPanel._sceneToLoad;
        }
        else {
            sceneName = "GameScene_1";
        }
        SpriteRenderer sp = GameObject.Find("bgimg").GetComponent<SpriteRenderer>();
        Texture2D texture2d = (Texture2D)Resources.Load("sence" + sceneName.Split('_')[1]);
        sp.sprite = Sprite.Create(texture2d, sp.sprite.textureRect, new Vector2(0.5f, 0.5f));
        GObject[] via = GRoot.inst.GetChildren();
        for (int i = 0; i < via.Length; i++)
        {
            ((GObject)via[i]).Dispose();
        }
        UIControl.instance.Create(); 
        GameObject obj = GameObject.Find("Stage");
        if (obj != null) {
            audiosource = obj.GetComponent<AudioSource>();
        }
       
    }

    // Update is called once per frame
    void Update()
    {
        // // 成功
        // if (PlayerInput.Instance.LClick.Down)
        // {
        //     Debug.Log(PlayerInput.Instance.LClick.Down);
        //     UIControl.instance.playState(true);
        // }
        // //失敗 com清0
        // if (PlayerInput.Instance.RClick.Down)
        // {
        //     Debug.Log(PlayerInput.Instance.RClick.Down);
        //     UIControl.instance.playState(false);
        // } 
        // //清除分數
        // if (PlayerInput.Instance.Jump.Down)
        // {
        //     Debug.Log(PlayerInput.Instance.Jump.Down);
        //     UIControl.instance.clearScore();
        // }
        //动画进入下一场景
        if (GameManager.IsSongCompleted)
        {
            GameManager.IsSongCompleted = false;
            if (sceneName == "GameScene_1") {
                playFrame1();
            }else if (sceneName == "GameScene_2")
            {
                playFrame4();
            }
            else if (sceneName == "GameScene_3")
            {
                playFrame8();
            }
            else if (sceneName == "GameScene_4")
            {
                playFrame11();
            }
        }
    } 
    public void playFrame1()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame1").asCom;
        win.Show();
        win.contentPane.GetTransition("danru").Play();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GImage b = vcom.GetChild("b").asImage;
        a.onClick.Add(() =>
        {
            StartCoroutine(PlayMusicByName("s1_1", 0f));
            a.visible = false;
            b.visible = true;
            playOrgin(win.contentPane,2);
        });
    }
    public void playFrame2()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame2").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GLoader a = vcom.GetChild("a").asLoader;
        GButton b = vcom.GetChild("b").asButton;
        a.draggable = true;
        PlayDragDrop(win.contentPane,a, b,3);
    }
    public void playFrame3()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame3").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GButton b = vcom.GetChild("b").asButton;
        GImage c = vcom.GetChild("c").asImage;
        PlayDragDrop3(win.contentPane,a, b, c,4);
    }
    public void playFrame4()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame4").asCom;
        win.Show();
        win.contentPane.GetTransition("danru").Play();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GLoader a = vcom.GetChild("a").asLoader;
        GButton b = vcom.GetChild("b").asButton;
        a.draggable = true;
        PlayDragDrop(win.contentPane,a, b, 5);
    }

    public void playFrame5()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame5").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GLoader a = vcom.GetChild("a").asLoader;
        GLoader b = vcom.GetChild("b").asLoader;
        GButton c = vcom.GetChild("c").asButton;
        GButton d = vcom.GetChild("d").asButton;
        PlayDragDrop5(win.contentPane,a, b, c, d,6);
    }
    public void playFrame6()
    {
        Window win = new Window(); 
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame6").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GImage b = vcom.GetChild("b").asImage;
        a.onClick.Add(() =>
        {
            a.visible = false;
            b.visible = true;
            this.playOrgin(win.contentPane, 7);
        });
    }
    public void playFrame7()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame7").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GLoader a = vcom.GetChild("a").asLoader;
        GLoader b = vcom.GetChild("b").asLoader;
        GButton c = vcom.GetChild("c").asButton;
        GButton d = vcom.GetChild("d").asButton;
        PlayDragDrop7(win.contentPane,a, b, c, d,8);
    }
    public void playFrame8()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame8").asCom;
        win.Show();
        win.contentPane.GetTransition("danru").Play();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GImage b = vcom.GetChild("b").asImage;
        a.onClick.Add(() =>
        {
            a.visible = false;
            b.visible = true;

            this.playOrgin(win.contentPane, 9);
        });
       
    }
    public void playFrame9()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame9").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GImage b = vcom.GetChild("b").asImage;
        a.onClick.Add(() =>
        {
            a.visible = false;
            b.visible = true;
            this.playOrgin(win.contentPane, 10);
        });
        
    }
    public void playFrame10()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame10").asCom;
        win.Show();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        GButton a = vcom.GetChild("a").asButton;
        GImage b = vcom.GetChild("b").asImage;
        a.onClick.Add(() =>
        {
            a.visible = false;
            b.visible = true;
        });
        this.playOrgin(win.contentPane, 11);
    }
    public void playFrame11()
    {
        Window win = new Window();
        win.contentPane = UIPackage.CreateObject("imcui", "Sgame11").asCom;
        win.Show();
        win.contentPane.GetTransition("danru").Play();
        GComponent vcom = win.contentPane.GetChild("frame").asCom;
        Transition ats = vcom.GetTransition("scale");
        ats.Play(()=> {
            GObject[] via = GRoot.inst.GetChildren();
            for (int i = 0; i < via.Length; i++)
            {
                ((GObject)via[i]).Dispose();
            }
            LoadingPanel.LoadSceneAsync("StartScene");
        });
    }
    private void PlayDragDrop(GComponent com, GLoader b, GButton c,int index)
    {
        b.draggable = true;
        b.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(b, b.icon, b.icon, (int)context.data);
        });
        c.onDrop.Add((EventContext context) =>
        {
            c.icon = (string)context.data;
            b.visible = false;
            playOrgin(com, index);
        });
    }
    private void PlayDragDrop3(GComponent com, GButton a, GButton b, GImage c,int index)
    {
        a.draggable = true;
        a.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(a, a.icon, a.icon, (int)context.data);
        });
        b.draggable = true;
        b.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent. 
            context.PreventDefault();

            DragDropManager.inst.StartDrag(b, b.icon, b.icon, (int)context.data);
        });
        a.onDrop.Add((EventContext context) =>
        {
            a.visible = false;
            b.visible = false;
            c.visible = true;
            this.playOrgin(com, index);
        });
        b.onDrop.Add((EventContext context) =>
        {
            a.visible = false;
            b.visible = false;
            c.visible = true;
            this.playOrgin(com, index);
        });
    }
    private void PlayDragDrop5(GComponent com,GLoader a, GLoader b, GButton c, GButton d,int index)
    {
        bool flaga = false;
        bool flagb = false;
        a.draggable = true;
        a.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(a, a.icon, a.icon, (int)context.data);
        });
        b.draggable = true;
        b.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(b, b.icon, b.icon, (int)context.data);
        });
        c.onDrop.Add((EventContext context) =>
        {
            if (context.data != b.icon) {
                EventContext context1 = context;
                a.visible = false;
                c.icon = (string)context.data;
                this.playOrgin(com, index);
            }
        });
        d.onDrop.Add((EventContext context) =>
        {
            if (context.data != a.icon)
            {
                EventContext context1 = context;
                b.visible = false;
                d.icon = (string)context.data;
                this.playOrgin(com, index);
            }
        });
    }

    private void PlayDragDrop7(GComponent com, GLoader a, GLoader b, GButton c, GButton d, int index)
    {
        bool flaga = false;
        bool flagb = false;
        a.draggable = true;
        a.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(a, a.icon, a.icon, (int)context.data);
        });
        b.draggable = true;
        b.onDragStart.Add((EventContext context) =>
        {
            //Cancel the original dragging, and start a new one with a agent.
            context.PreventDefault();

            DragDropManager.inst.StartDrag(b, b.icon, b.icon, (int)context.data);
        });
        c.onDrop.Add((EventContext context) =>
        {
            if (context.data != b.icon)
            {
                EventContext context1 = context;
                a.visible = false;
                c.icon = (string)context.data;
                flagb = true;
                if (flagb && flaga)
                {
                    this.playOrgin(com, index);
                }
            }
        });
        d.onDrop.Add((EventContext context) =>
        {
            if (context.data != a.icon)
            {
                EventContext context1 = context;
                b.visible = false;
                d.icon = (string)context.data;
                flaga = true;
                if (flagb && flaga)
                {
                    this.playOrgin(com, index);
                }
            }
        });
    }
    private void playOrgin(GComponent gc,int index) {
        if (index >= 2) {
            StartCoroutine(PlayMusicByName("s1_2", 0f));
        }
        Transition ts = gc.GetTransition("danchu");
        ts.Play(()=> {
            switch (index) {
                case 1:playFrame1();break;
                case 2: playFrame2(); break;
                case 3: playFrame3(); break;
                case 4: LoadingPanel.LoadSceneAsync("GameScene_"+2) ;  break;
                case 5: playFrame5(); break;
                case 6: playFrame6(); break;
                case 7: playFrame7(); break;
                case 8: LoadingPanel.LoadSceneAsync("GameScene_" + 3);  break;
                case 9: playFrame9(); break;
                case 10: playFrame10(); break;
                case 11: LoadingPanel.LoadSceneAsync("GameScene_" + 4); ; break;
            }
        });
    }
    private IEnumerator goToNextScene()
    {
        yield return new WaitForSeconds(4f);

        LoadingPanel.LoadSceneAsync("GameScene_" + (Convert.ToInt32(sceneName.Split('_')[1]) +1));
    }

    private IEnumerator PlayMusicByName(string name,float delay)
    {
        yield return new WaitForSeconds(delay);
        AudioClip clip = Resources.Load<AudioClip>("Sounds/" + name);
        if (audiosource == null)
        {
        }
        else {
            if (audiosource.isPlaying)
            {
                audiosource.Stop();
            }

            audiosource.clip = clip;
            audiosource.Play();
        }
       
    }
}
